package com.wlhse.service.impl;


import com.github.pagehelper.PageHelper;
import com.wlhse.dao.QHSEECheckRecordDao;
import com.wlhse.dao.QHSEERectRecordDao;
import com.wlhse.dao.UserDao;
import com.wlhse.dao.UserRoleDao;
import com.wlhse.dto.*;
import com.wlhse.dto.inDto.CheckRecordIDDto;
import com.wlhse.dto.outDto.Sys_rolesDto;
import com.wlhse.entity.CheckPersonPojo;
import com.wlhse.entity.QHSEECheckRecordPojo;
import com.wlhse.entity.QHSEERectRecordPojo;
import com.wlhse.entity.UserRolePojo;
import com.wlhse.service.HSECheckRecordService;
import com.wlhse.util.DictCode;
import com.wlhse.util.GetCurrentUserIdUtil;
import com.wlhse.util.R;
import com.wlhse.util.state_code.CodeDict;
import com.wlhse.util.state_code.NR;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Service
public class HSECheckRecordServiceImpl implements HSECheckRecordService {
    @Resource
    private QHSEECheckRecordDao qhseeCheckRecordDao;

    @Resource
    private GetCurrentUserIdUtil getCurrentUserIdUtil;

    @Resource
    private UserRoleDao userRoleDao;

    @Resource
    private UserDao userDao;

    @Resource
    private QHSEERectRecordDao qhseeRectRecordDao;


    @Override
    public String queryQHSEECheckRecordByHeadman(HseCheckRecordDto hseCheckRecordDto, HttpServletRequest request) {
        //根据登录人的不同来查看不同的审核记录
        Integer userid=getCurrentUserIdUtil.getUserId(request);
        hseCheckRecordDto.setUserID(userid);

        if (hseCheckRecordDto.getStartDate().equals("")){
            hseCheckRecordDto.setStartDate("1900-01-01");
        }
        if (hseCheckRecordDto.getEndDate().equals("")){
            hseCheckRecordDto.setEndDate("3000-01-01");
        }

        //组长查看
        if (null==hseCheckRecordDto.getCompanyCode()||"".equals(hseCheckRecordDto.getCompanyCode())){
            int total = qhseeCheckRecordDao.queryQHSEECheckRecordByInternalAuditorID2(hseCheckRecordDto).size();
            PageHelper.startPage(hseCheckRecordDto.getNum(),hseCheckRecordDto.getSize());
            List<QHSEECheckRecordPojo> res = qhseeCheckRecordDao.queryQHSEECheckRecordByInternalAuditorID2(hseCheckRecordDto);
            return NR.r(res,total,hseCheckRecordDto.getNum());
        }
        else {
            int total = qhseeCheckRecordDao.queryQHSEECheckRecordByInternalAuditorID1(hseCheckRecordDto).size();
            PageHelper.startPage(hseCheckRecordDto.getNum(),hseCheckRecordDto.getSize());
            List<QHSEECheckRecordPojo> res = qhseeCheckRecordDao.queryQHSEECheckRecordByInternalAuditorID1(hseCheckRecordDto);
            return NR.r(res,total,hseCheckRecordDto.getNum());
        }

    }

    @Override
    public String queryQHSEECheckRecordByAdmin(HseCheckRecordDto hseCheckRecordDto, HttpServletRequest request) {
        Integer userid=getCurrentUserIdUtil.getUserId(request);
        hseCheckRecordDto.setUserID(userid);

        if (hseCheckRecordDto.getStartDate().equals("")){
            hseCheckRecordDto.setStartDate("1900-01-01");
        }
        if (hseCheckRecordDto.getEndDate().equals("")){
            hseCheckRecordDto.setEndDate("3000-01-01");
        }

        //管理员查看所有组长已审核的记录
        if (null==hseCheckRecordDto.getCompanyCode()||"".equals(hseCheckRecordDto.getCompanyCode())){
            int total = qhseeCheckRecordDao.queryQHSEECheckRecordByManagementAuditorID2(hseCheckRecordDto).size();
            PageHelper.startPage(hseCheckRecordDto.getNum(),hseCheckRecordDto.getSize());
            List<QHSEECheckRecordPojo> res = qhseeCheckRecordDao.queryQHSEECheckRecordByManagementAuditorID2(hseCheckRecordDto);
            return NR.r(res,total,hseCheckRecordDto.getNum());
        }
        else {
            int total = qhseeCheckRecordDao.queryQHSEECheckRecordByManagementAuditorID1(hseCheckRecordDto).size();
            PageHelper.startPage(hseCheckRecordDto.getNum(),hseCheckRecordDto.getSize());
            List<QHSEECheckRecordPojo> res = qhseeCheckRecordDao.queryQHSEECheckRecordByManagementAuditorID1(hseCheckRecordDto);
            return NR.r(res,total,hseCheckRecordDto.getNum());
        }
    }

    @Override
    @Transactional
    public String updateRecordStatusByHeadman(QHSEECheckRecordPojo qhseeCheckRecordPojo, HttpServletRequest request) {
        //判断是谁点击的审核
        Integer userid=getCurrentUserIdUtil.getUserId(request);
        String uName = userDao.getUserName((userid.toString()));

        QHSEECheckRecordPojo checkRecord = qhseeCheckRecordDao.queryCheckRecord(qhseeCheckRecordPojo.getQHSEECheckRecordID());

        if(checkRecord.getRecordStatus()==null||checkRecord.getRecordStatus().equals("")){
            checkRecord.setRecordStatus("未审核");
        }

        if (userid.equals(checkRecord.getInternalAuditorID())){
            //该组组长进行审核
            if(checkRecord.getRecordStatus().equals("未审核")){
                //组长已审核
                qhseeCheckRecordPojo.setRecordStatus("组长已审核");
                qhseeCheckRecordPojo.setInternalAuditorID(userid);
                qhseeCheckRecordPojo.setInternalAuditorName(uName);
                qhseeCheckRecordDao.updateRecordStatus(qhseeCheckRecordPojo);
                return NR.r();
            }
            return NR.r(CodeDict.UPDATE_FAILE,"审核失败，当前记录状态为"+checkRecord.getRecordStatus());
        }else {
            return NR.r(CodeDict.UPDATE_FAILE,"权限不足，请等待该组组长操作");
        }
    }

    @Override
    public String updateRecordStatusByAdmin(QHSEECheckRecordPojo qhseeCheckRecordPojo, HttpServletRequest request) {
        //判断是谁点击的审核
        Integer userid=getCurrentUserIdUtil.getUserId(request);
        String uName = userDao.getUserName((userid.toString()));

        QHSEECheckRecordPojo checkRecord = qhseeCheckRecordDao.queryCheckRecord(qhseeCheckRecordPojo.getQHSEECheckRecordID());

        if(checkRecord.getRecordStatus()==null||checkRecord.getRecordStatus().equals("")){
            checkRecord.setRecordStatus("未审核");
        }

        //管理层进行审核
        if (checkRecord.getRecordStatus().equals("组长已审核")){
            //RecordStatus修改为 管理员已审核
            qhseeCheckRecordPojo.setRecordStatus("管理员已审核");
            qhseeCheckRecordPojo.setManagementAuditorID(userid);
            qhseeCheckRecordPojo.setManagementAuditorName(uName);
            qhseeCheckRecordDao.updateRecordStatus(qhseeCheckRecordPojo);
            return NR.r();
        }
        return NR.r(CodeDict.UPDATE_FAILE,"审核失败，当前记录状态为"+checkRecord.getRecordStatus());

    }

    @Override
    public String deleteCheckRecord(Integer qHSEECheckRecordID) {
        qhseeCheckRecordDao.deleteCheckRecord(qHSEECheckRecordID);
        return NR.r();
    }

    @Override
    public String queryCheckRecordByID(HseCheckRecordDto hseCheckRecordDto,HttpServletRequest request) {
        //获取当前登录人的id
        Integer userid=getCurrentUserIdUtil.getUserId(request);
        //只显示与当前登录人id一致的整改责任人

        hseCheckRecordDto.setUserID(userid);

        if (hseCheckRecordDto.getStartDate().equals("")){
            hseCheckRecordDto.setStartDate("1900-01-01");
        }
        if (hseCheckRecordDto.getEndDate().equals("")){
            hseCheckRecordDto.setEndDate("3000-01-01");
        }

        if (hseCheckRecordDto.getCompanyCode().equals("")){
            int total = qhseeCheckRecordDao.queryCheckRecord2(hseCheckRecordDto).size();
            PageHelper.startPage(hseCheckRecordDto.getNum(),hseCheckRecordDto.getSize());
            List<QHSEECheckRecordPojo> res = qhseeCheckRecordDao.queryCheckRecord2(hseCheckRecordDto);
            return NR.r(res,total,hseCheckRecordDto.getNum());
        }
        else {
            int total = qhseeCheckRecordDao.queryCheckRecord1(hseCheckRecordDto).size();
            PageHelper.startPage(hseCheckRecordDto.getNum(),hseCheckRecordDto.getSize());
            List<QHSEECheckRecordPojo> res = qhseeCheckRecordDao.queryCheckRecord1(hseCheckRecordDto);
            return NR.r(res,total,hseCheckRecordDto.getNum());
        }

    }

    @Override
    public String updateRectCheckRecord(RectCheckRecordDto rectCheckRecordDto,HttpServletRequest request) {
        Integer userid=getCurrentUserIdUtil.getUserId(request);
        String name = userDao.queryEmployeeNameByid((userid)); //姓名

        //qhseeRectRecord记录赋值
        QHSEERectRecordPojo qhseeRectRecordPojo=new QHSEERectRecordPojo();
        qhseeRectRecordPojo.setQHSEECheckRecordID(rectCheckRecordDto.getQHSEECheckRecordID());
        qhseeRectRecordPojo.setSupervisorID(userid);
        qhseeRectRecordPojo.setSupervisorName(name);
        qhseeRectRecordPojo.setRectAttach1(rectCheckRecordDto.getRectAttach1());
        qhseeRectRecordPojo.setRectAttach2(rectCheckRecordDto.getRectAttach2());
        qhseeRectRecordPojo.setRectAttach3(rectCheckRecordDto.getRectAttach3());
        qhseeRectRecordPojo.setRectAttach4(rectCheckRecordDto.getRectAttach4());
        qhseeRectRecordPojo.setRectDescription(rectCheckRecordDto.getRectDescription());
        qhseeRectRecordPojo.setRectVideo(rectCheckRecordDto.getRectVideo());
        qhseeRectRecordPojo.setRectFile(rectCheckRecordDto.getRectFile());
        qhseeRectRecordPojo.setRectDate(rectCheckRecordDto.getRectDate());

        //添加整改记录
        qhseeRectRecordDao.addQHSEERectRecord(qhseeRectRecordPojo);

        //修改checkrecord中整改的部分
        qhseeCheckRecordDao.updateRectCheckRecord(rectCheckRecordDto);
        return NR.r();
    }

    @Override
    public String addCheckperson(CheckPersonDto checkPersonDto) {
        for(CheckPerson checkPerson :checkPersonDto.getCheckPersons()){
            CheckPersonPojo person = qhseeCheckRecordDao.getCheckPerson(checkPerson.getCheckPersonID());
            if (person!=null){
                return NR.r(CodeDict.INSERT_FAILE,"添加失败，"+person.getCheckPersonName()+"已存在");
            }
            qhseeCheckRecordDao.addCheckperson(checkPerson);
            String Uname=qhseeCheckRecordDao.queryRole(checkPerson.getCheckPersonID());
            if(null!=Uname)qhseeCheckRecordDao.addCheckpersonRole(Uname,checkPerson.getRoleCode());

        }
        return NR.r();
    }

    @Override
    public String updateCheckperson(CheckPersonDto checkPersonDto) {
        for(CheckPerson checkPerson :checkPersonDto.getCheckPersons()){
            qhseeCheckRecordDao.updateCheckperson(checkPerson);

            String Uname=qhseeCheckRecordDao.queryRole(checkPerson.getCheckPersonID());
            UserRolePojo userRolePojo = userRoleDao.queryQHSERoleCodeByUname(Uname);
            userRolePojo.setRoleCode(checkPerson.getRoleCode());
            userRolePojo.setUName(Uname);
            if(!"".equals(Uname)||null!=Uname)qhseeCheckRecordDao.updateCheckpersonRole(userRolePojo);

        }
        return NR.r();
    }

    @Override
    public String queryAllCheckPerson(Integer num,Integer size) {
        int total = qhseeCheckRecordDao.queryAllCheckPerson().size();
        PageHelper.startPage(num,size);
        List<CheckPersonPojo> checkPersons = qhseeCheckRecordDao.queryAllCheckPerson();
        return NR.r(checkPersons,total,num);
    }

    @Override
    public String queryHeadman(Integer num, Integer size) {
        int total = qhseeCheckRecordDao.queryHeadman().size();
        PageHelper.startPage(num,size);
        List<CheckPersonPojo> checkPersons = qhseeCheckRecordDao.queryHeadman();
        return NR.r(checkPersons,total,num);
    }

    @Override
    public String queryMember(Integer num, Integer size) {
        int total = qhseeCheckRecordDao.queryMember().size();
        PageHelper.startPage(num,size);
        List<CheckPersonPojo> checkPersons = qhseeCheckRecordDao.queryMember();
        return NR.r(checkPersons,total,num);
    }

    @Override
    public String deleteCheckPerson(Integer checkPersonID) {
        int count = qhseeCheckRecordDao.deleteCheckPerson(checkPersonID);
        if (count<1){
            return NR.r(CodeDict.DELETE_FAILE,"删除失败，该审核人不存在");
        }
        return NR.r();
    }

    @Override
    public String updateAllRecordStatusByByHeadman(CheckRecordIDDto checkRecordIDDto, HttpServletRequest request) {
        //判断是谁点击的审核
        Integer userid=getCurrentUserIdUtil.getUserId(request);

        List<Integer> qHSEECheckRecordIDs=checkRecordIDDto.getQHSEECheckRecordIDs();
        System.out.println(qHSEECheckRecordIDs);

        if(qHSEECheckRecordIDs.size()>0){
            for(Integer id:qHSEECheckRecordIDs){
                QHSEECheckRecordPojo checkRecord = qhseeCheckRecordDao.queryCheckRecord(id);
                if(checkRecord.getRecordStatus()==null||checkRecord.getRecordStatus().equals("")){
                    checkRecord.setRecordStatus("未审核");
                }

                if (userid.equals(checkRecord.getInternalAuditorID())){
                    //该组组长进行审核
                    if(!checkRecord.getRecordStatus().equals("未审核")){
                        return NR.r(CodeDict.UPDATE_FAILE,"审核失败，当前记录状态为"+checkRecord.getRecordStatus());
                    }
                    checkRecord.setRecordStatus("组长已审核");
                    qhseeCheckRecordDao.updateRecordStatus(checkRecord);
                }else {
                    return NR.r(CodeDict.UPDATE_FAILE,"权限不足，请等待该组组长操作");
                }
            }
        }
        return NR.r();
    }

    @Override
    public String updateAllRecordStatusByAdmin(CheckRecordIDDto checkRecordIDDto, HttpServletRequest request) {
        //判断是谁点击的审核
        Integer userid=getCurrentUserIdUtil.getUserId(request);
        String name = userDao.queryEmployeeNameByid((userid)); //姓名

        List<Integer> qHSEECheckRecordIDs=checkRecordIDDto.getQHSEECheckRecordIDs();

        if(qHSEECheckRecordIDs.size()>0){
            for(Integer id:qHSEECheckRecordIDs){

                QHSEECheckRecordPojo checkRecord = qhseeCheckRecordDao.queryCheckRecord(id);
                if(checkRecord.getRecordStatus()==null||checkRecord.getRecordStatus().equals("")){
                    checkRecord.setRecordStatus("未审核");
                }

                //管理层进行审核
                if (!checkRecord.getRecordStatus().equals("组长已审核")){
                    return NR.r(CodeDict.UPDATE_FAILE,"审核失败，当前记录状态为"+checkRecord.getRecordStatus());
                }
                //RecordStatus修改为 管理员已审核
                checkRecord.setRecordStatus("管理员已审核");
                checkRecord.setManagementAuditorID(userid);
                checkRecord.setManagementAuditorName(name);
                qhseeCheckRecordDao.updateRecordStatus(checkRecord);
            }
        }
        return NR.r();
    }

    @Override
    @Transactional
    public String updatePersonRole(CheckPerson checkPerson) {
        if (checkPerson.getCheckPersonID() != null) {
            qhseeCheckRecordDao.updatePersonRole(checkPerson);
            return NR.r();
        }
        return NR.r(CodeDict.UPDATE_FAILE,"权限不足，请等待该组组长和管理员操作");
    }

    @Override
    @Transactional
    public String deleteCheckPersons(List<String> checkPersonIDList) {
        if(checkPersonIDList.size()>0){
            int i = 0;
            i = qhseeCheckRecordDao.deleteCheckPersons(checkPersonIDList);
            if (i>0){
                return NR.r();
            }
        }
        return NR.r(CodeDict.UPDATE_FAILE,"未选中，删除未成功");
    }

    @Override
    public R queryNameLikeQhsee() {
        return R.ok().put("data",qhseeCheckRecordDao.queryNameLikeQhsee());
    }
}
